package com.hall.service;

import com.hall.dao.AttentionMapper;
import com.hall.dao.UserMapper;
import com.hall.pojo.Appointment;
import com.hall.pojo.R;
import com.hall.pojo.SysUser;
import com.hall.pojo.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public class AttentionServesImpl implements AttentionServes {
    @Autowired
    AttentionMapper attentionMapper;
    @Autowired
    UserMapper userMapper;

    public void setAttentionMapper(AttentionMapper attentionMapper) {
    }

    @Override
    public List<SysUser> findAttentionById(String id, int is_del, String name) {
        return attentionMapper.findAttentionById(id, is_del, name);
    }

    @Override
    public List<Appointment> findCollectById(String id, int is_del) {
        return attentionMapper.findCollectById(id, is_del);
    }

    @Override
    public int cancelAttentionById(int is_del, String id, String loginid) {
        return attentionMapper.cancelAttentionById(is_del, id, loginid);
    }

    @Override
    public int cancelCollectById(int is_del, String id, String loginid) {
        return attentionMapper.cancelCollectById(is_del, id, loginid);
    }

    @Override
    public int addCollect(String appointId, String userId) {
        return attentionMapper.addCollect(appointId, userId);
    }

    @Override
    public Result addAttention(String userid, String beUserid) {
        if(userid==null || "".equals(userid)) return Result.error("不存在的用户,请重试");
        if (userMapper.getUser(userid)== null || userMapper.getUser(beUserid)==null) {
            return Result.error("不存在的用户,请重试");
        }
        //查询当前id关注的所有用户
        List<String> allUser = attentionMapper.attentionIdList(userid);
        System.out.println(allUser);
        //如果关注过当前用户则不执行二次关注
        if (allUser.contains(beUserid)) {
            return Result.error("当前用户已关注");
        }
        //当前用户是否是曾经关注过再取关的
        if (attentionMapper.pastselects(beUserid,userid) > 0) {
            //如果是,直接修改状态
            boolean check = attentionMapper.updateAttention(beUserid,userid) > 0;
            return check ? Result.success(null, "关注成功") : Result.error("关注失败");
        } else {
            //不是则新增
            boolean checks = attentionMapper.addAttention(beUserid,userid) > 0;
            return checks ? Result.success(null, "关注成功") : Result.error("关注失败");
        }
    }

    @Override
    public List<String> attentionIdList(String id) {
        return attentionMapper.attentionIdList(id);
    }

    @Override
    public List<String> isCollect(String id) {
        return attentionMapper.isCollect(id);
    }

    @Override
    public List<SysUser> selectAttention(String id, int is_del, String index) {
        return attentionMapper.selectAttention(id, is_del, index);
    }

    @Override
    public List<Appointment> selectCollect(String id, int is_del, String index) {
        return attentionMapper.selectCollect(id, is_del, index);
    }

    @Override
    public int pastselect(String appointId, String userId) {
        return attentionMapper.pastselect(appointId, userId);
    }

    @Override
    public List<SysUser> findFansList(String id) {
        return attentionMapper.findFansList(id);
    }


    public int updatecollect(String appointId, String userId) {
        return attentionMapper.updatecollect(appointId, userId);
    }

}
